﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_AddName : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        ZSql rs = new ZSql();
        if (!IsPostBack)
        {
            lblTime.Text = DateTime.Now.ToShortDateString();
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            HidGoUrl.Value = Server.UrlDecode(Request.UrlReferrer.ToString());
            lblwz.Text = f.getModuleidAdd(Request["moduid"]);
            hidModuid.Value = Request.QueryString["moduid"];
            Hidid.Value = Request.QueryString["id"];
            initUnit();//单位
            initType();
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))          
            {
                string sql = "select top 1 * from SV_Catalogue where catalogueId=" + Request.QueryString["id"];
                rs.Open(sql);
                if (rs.m_table.Rows.Count > 0)
                {
                    catalogueName.Text = rs.m_table.Rows[0]["catalogueName"].ToString();
                    ComodBarcode.Text = rs.m_table.Rows[0]["ComodBarcode"].ToString();//条码
                    ddlType.SelectedValue = rs.m_table.Rows[0]["TypeId"].ToString();
                    ddlUnits.SelectedValue = rs.m_table.Rows[0]["Units"].ToString();//单位
                    Spec.Text = rs.m_table.Rows[0]["Spec"].ToString();//进库数量
                    ProcMoney.Text = rs.m_table.Rows[0]["ProcMoney"].ToString();//进货价
                    Prices.Text = rs.m_table.Rows[0]["Prices"].ToString();//销售价
                    Remark.Text = rs.m_table.Rows[0]["Remark"].ToString();//备注
                    lblUser.Text = rs.m_table.Rows[0]["EditUserName"].ToString();//编辑人
                    lblTime.Text = rs.m_table.Rows[0]["EditDate"].ToString();//编辑时间
                    if (rs.m_table.Rows[0]["AuditState"].ToString() == "1") AuditState.Checked = true; else AuditState1.Checked = true;
                }
                rs.Close();
            }
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string search = "";
        string sqlsearch = "select * from SV_Catalogue where ComodBarcode='" + Common.FormatParameter(ComodBarcode.Text.Trim()) + "' ";
        if (!string.IsNullOrEmpty(Hidid.Value)) {//修改、审核时,要除去本条记录进行判断！
            search = " and CatalogueId<>" + Hidid.Value;
            sqlsearch += search;
        }
        //Response.Write(sqlsearch);
        //Response.End();
        ZSql zs = new ZSql();
        zs.Open(sqlsearch);
        if (zs.m_table.Rows.Count > 0)
        {
            Response.Write("<script>alert('该目录名称或者编号条码已存在,请查询后再编辑!');location.href=document.referrer</script>");
            return;

        }
        else
        {
            ZSql info = new ZSql();
            string action = Request.QueryString["action"];
            string sql = "";
            if (action == "add")//新增
                sql = "select top 1 * from SV_Catalogue";
            else//修改
                sql = "select top 1 * from SV_Catalogue where catalogueId=" + Request.QueryString["id"];

            info.Open(sql);
            info.NextRow();
            if (action == "add")//新增
            {
                info.NewRow();
                info["catalogueId"] = Convert.ToInt32(f.getMaxId("SV_Catalogue", "catalogueId")) + 1;
            }

            info["catalogueName"] = catalogueName.Text;//名称
            info["ComodBarcode"] = ComodBarcode.Text;//条码
            info["TypeId"] = ddlType.SelectedValue;//类型ID
            info["ProcMoney"] = ProcMoney.Text;//进货价
            info["Prices"] = Prices.Text;//销售价
            info["Units"] = ddlUnits.SelectedValue;//单位ID
            info["Spec"] = Spec.Text;//进库数量
            info["Remark"] = Remark.Text;//备注

            info["AuditState"] = Request.Form["AuditState"];
            info["EditUserName"] = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);//编辑人
            info["EditDate"] = DateTime.Now.ToString();//时间

            info.Update();
            info.Close();
            Response.Write("<script>alert('操作已成功，请返回！');</script>");//返回页面
            Response.Write("<script>window.location='" + HidGoUrl.Value + "';</script>");//返回页面
        }

    }

    private void initUnit()//单位
    {
        ZSql zUnit = new ZSql();

        zUnit.Open("select * from Units where AuditState=1 order by UnitsOrder");
        ddlUnits.DataSource = zUnit.m_table;
        ddlUnits.DataTextField = "Units";
        ddlUnits.DataValueField = "UnitsId";
        ddlUnits.DataBind();
        zUnit.Close();
    }
    private void initType()
    {
        ZSql type = new ZSql();
        type.Open("select * from SV_Type where AuditState=1");
        if (type.m_table.Rows.Count > 0)
        {
            ddlType.DataSource = type.m_table;
            ddlType.DataTextField = "TypeName";
            ddlType.DataValueField = "TypeId";
            ddlType.DataBind();
            ddlType.Items.Insert(0, new ListItem("—选择商品类型—", ""));
            type.Close();
        }
    }



}
